 package gui.settings;
 
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class ConfigFile {
	
	private static ConfigFile instance ;
	
	private  Integer	TERM_NUM;
	private  Integer	TERM_MIN_FREQ;
	private  boolean	ADD_SYNONYMS;  
	private  boolean	STEMMING_RESULTS;  
	
	public final String fileName = "config.xml";
	
	private ConfigFile(){
		
		try {
			
	    	DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
	        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
	        Document doc = docBuilder.parse (new File("config.xml"));
	
            NodeList nl_term_num = doc.getElementsByTagName("TERM_NUM");
            Element TN = ((Element)nl_term_num.item(0));
            NodeList textTNList = TN.getChildNodes();
            TERM_NUM = Integer.parseInt(((Node)textTNList.item(0)).getNodeValue().trim());
            
            NodeList nl_term_min_freq = doc.getElementsByTagName("TERM_MIN_FREQ");
            Element TMF = ((Element)nl_term_min_freq.item(0));
            NodeList textTMFList = TMF.getChildNodes();
            TERM_MIN_FREQ = Integer.parseInt(((Node)textTMFList.item(0)).getNodeValue().trim());
            
            NodeList nl_add_synonym = doc.getElementsByTagName("ADD_SYNONYMS");
            Element AS = ((Element)nl_add_synonym.item(0));
            NodeList textASList = AS.getChildNodes();
            ADD_SYNONYMS = Boolean.parseBoolean(((Node)textASList.item(0)).getNodeValue().trim());
            
            NodeList nl_stemming_results = doc.getElementsByTagName("STEMMING_RESULTS");
            Element SR = ((Element)nl_stemming_results.item(0));
            NodeList textSRList = SR.getChildNodes();
            STEMMING_RESULTS = Boolean.parseBoolean(((Node)textSRList.item(0)).getNodeValue().trim());
            
        }catch (java.io.FileNotFoundException fnfe) {
        	
			assegnaValoriDefault();
			creaFileXML();
			
        }catch (SAXParseException err) {
        	
	        System.out.println ("** Parsing error" + ", line " 
	             + err.getLineNumber () + ", uri " + err.getSystemId ());
	        System.out.println(" " + err.getMessage ());

        }catch (SAXException e) {
        	
	        Exception x = e.getException ();
	        ((x == null) ? e : x).printStackTrace ();

        }catch (Throwable t) {
        	
        	t.printStackTrace ();
        	
        }
	}
	
	public static ConfigFile getInstance() {
		
		if (instance == null)
			instance = new ConfigFile() ;	
		return instance ;
	}
	
	private void creaFileXML(){
		File xmlFile = new File("config.xml");
		
		try{
			xmlFile.createNewFile();
			FileWriter fw = new FileWriter(xmlFile);
			BufferedWriter bw = new BufferedWriter(fw);
			
			bw.write("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>"); 
			bw.write("<lbd>");
			bw.write("<TERM_NUM>5</TERM_NUM>"); 
			bw.write("<TERM_MIN_FREQ>2</TERM_MIN_FREQ>"); 
			bw.write("<ADD_SYNONYMS>true</ADD_SYNONYMS>"); 
			bw.write("<STEMMING_RESULTS>true</STEMMING_RESULTS>"); 
			bw.write("</lbd>");
			  
			bw.flush();
			bw.close();
			
		}catch(Exception e){
			
		}
	}
	
	private void assegnaValoriDefault(){
		
		TERM_NUM = 5;
		TERM_MIN_FREQ = 2;
		ADD_SYNONYMS = true;  
		STEMMING_RESULTS = true; 
		
	}
	
	public void save(){
		
		File toDelete= new File("config.xml");
    	toDelete.delete();
		
		File xmlFile = new File("config.xml");
		
		try{
			
			xmlFile.createNewFile();
			FileWriter fw = new FileWriter(xmlFile);
			BufferedWriter bw = new BufferedWriter(fw);
			
			bw.write("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>"); 
			bw.write("<lbd>");
			bw.write("<TERM_NUM>" + TERM_NUM + "</TERM_NUM>"); 
			bw.write("<TERM_MIN_FREQ>" + TERM_MIN_FREQ + "</TERM_MIN_FREQ>"); 
			bw.write("<ADD_SYNONYMS>" + ADD_SYNONYMS + "</ADD_SYNONYMS>"); 
			bw.write("<STEMMING_RESULTS>" + STEMMING_RESULTS + "</STEMMING_RESULTS>"); 
			bw.write("</lbd>");
			  
			bw.flush();
			bw.close();
			
		}catch(Exception e){
			
		}
	}

	public  Integer getTERM_NUM() {
		return TERM_NUM;
	}

	public  Integer getTERM_MIN_FREQ() {
		return TERM_MIN_FREQ;
	}

	public  boolean isADD_SYNONYMS() {
		return ADD_SYNONYMS;
	}

	public  boolean isSTEMMING_RESULTS() {
		return STEMMING_RESULTS;
	}

	public void setTERM_NUM(Integer term_num) {
		TERM_NUM = term_num;
	}

	public void setTERM_MIN_FREQ(Integer term_min_freq) {
		TERM_MIN_FREQ = term_min_freq;
	}

	public void setADD_SYNONYMS(boolean add_synonyms) {
		ADD_SYNONYMS = add_synonyms;
	}

	public void setSTEMMING_RESULTS(boolean stemming_results) {
		STEMMING_RESULTS = stemming_results;
	}
	
}